<template>
  <div id="app">
    <TopBar :currentTitle="currentTitle" />
    <div class="container">
      <SidebarMenu @menu-item-clicked="onMenuItemClicked" />
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
import TopBar from './components/TopBar.vue';
import SidebarMenu from './components/SidebarMenu.vue';
import menuItems from './utils/menuItems.js'; // 引入 menuItems

export default {
  name: 'App',
  components: {
    TopBar,
    SidebarMenu
  },
  data() {
    return {
      currentTitle: ''
    };
  },
  created() {
    // 在页面加载时根据当前路由设置 currentTitle
    const currentPath = this.$router.currentRoute.path;
    const menuItem = this.findMenuItemByPath(currentPath);
    if (menuItem) {
      this.currentTitle = menuItem.title;
    }
  },
  methods: {
    onMenuItemClicked(item) {
      console.log('尝试导航到:', item.path);
      this.currentTitle = item.title;
      if (this.$router.currentRoute.path !== item.path) {
        this.$router.push(item.path);
      }
    },
    findMenuItemByPath(path) {
      for (const item of menuItems) {
        if (item.path === path) {
          return item;
        }
        if (item.children) {
          for (const subItem of item.children) {
            if (subItem.path === path) {
              return subItem;
            }
          }
        }
      }
      return null;
    }
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  /* text-align: center; */
  color: #2c3e50;
  min-width: 1630px;
}

.container {
  display: flex;
  /* text-align: center; */
}
</style>